package com.platform.enums;

import com.google.common.base.Strings;
import lombok.Getter;

import java.util.stream.Stream;

/**
 * Description: 主体类别
 *
 * @author libin Created on 2019/8/19
 **/
public enum DirMetadataPersonType {
    /**
     * NAT
     */
    NAT("自然人", "clz-dirClassify-sub-sub01", "A01"),
    /**
     * LEG
     */
    LEG("法人和非法人组织", "clz-dirClassify-sub-sub02", "A02"),
    /**
     * 自然人及法人或其他组织
     */
    MIX("混合类型", "mix_", "A03"),
    /**
     * 其他
     */
    OTH("其他类型", "clz-dirClassify-sub-sub03", "A04");

    @Getter
    private String text;

    @Getter
    private String prefix;

    @Getter
    private String code;

    DirMetadataPersonType(String text, String prefix, String code) {
        this.text = text;
        this.prefix = prefix;
        this.code = code;
    }

    public static DirMetadataPersonType parseByCode(String code) {
        if (code.startsWith(NAT.getPrefix())) {
            return NAT;
        } else if (code.startsWith(LEG.getPrefix())) {
            return LEG;
        } else if (code.startsWith(MIX.getPrefix())) {
            return MIX;
        } else if (code.startsWith(OTH.getPrefix())) {
            return OTH;
        }
        throw new RuntimeException("主体类别编码异常");
    }

    public static DirMetadataPersonType parseByPrefix(String tableName) {
        if (tableName.startsWith(NAT.getPrefix())) {
            return NAT;
        } else if (tableName.startsWith(LEG.getPrefix())) {
            return LEG;
        } else if (tableName.startsWith(MIX.getPrefix())) {
            return MIX;
        } else if (tableName.startsWith(OTH.getPrefix())) {
            return OTH;
        }
        throw new RuntimeException("表名缺失或不规范");
    }

    public enum BaseInfoType {
        /**
         * 主体识别信息
         */
        ID("主体识别信息", "id_info", "C0101"),
        DJXX("登记信息", "base_info", "C0102"),
        HJXX("户籍信息", "base_info", "C0103");
        @Getter
        private String text;

        @Getter
        private String suffix;

        @Getter
        private String code;

        BaseInfoType(String text, String suffix, String code) {
            this.text = text;
            this.suffix = suffix;
            this.code = code;
        }

        public static boolean checkExists(String code) {
            if (Strings.isNullOrEmpty(code)) {
                return false;
            }
            return Stream.of(values()).map(BaseInfoType::getCode).anyMatch(c -> c.equals(code));
        }
    }
}
